package com.ucar.train.web;

import com.ucar.train.dto.*;
import com.ucar.train.service.OrderService;
import com.ucar.train.service.impl.CrmAttentionServiceImpl;
import com.ucar.train.service.impl.StoreServiceImpl;
import com.ucar.train.vo.CarVO;
import com.ucar.train.vo.CrmAttentionVO;
import com.ucar.train.vo.CrmVO;
import com.ucar.train.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujinmu
 * @title StoreAttentionController
 * @projectName train-parent
 * @description TODO
 * @created 2019-08-27 13:54
 **/
@Controller
@RequestMapping("/storeAttention")
public class StoreAttentionController {

    @Autowired
    private CrmAttentionServiceImpl crmAttentionService;

    @Autowired
    private StoreServiceImpl storeService;

    @Autowired
    private OrderService orderService;

    /**
     * @Author zhoujinmu
     * @Description 显示所有用户
     * @Date 2019/8/27 17:11
     */
    @RequestMapping(value = "/showAllCrm", method = RequestMethod.GET)
    public ModelAndView addStoreAttention() {
        List<CrmVO> crmVOS = new ArrayList<>();
        List<CrmDTO> crmDTOS = crmAttentionService.getAllCustomer();
        System.out.println(crmDTOS);
        for (CrmDTO crmDTO : crmDTOS) {
            CrmVO crmVO = new CrmVO();
            crmVO.setId(crmDTO.getId());
            crmVO.setUsername(crmDTO.getUsername());
            crmVOS.add(crmVO);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customers", crmVOS);
        modelAndView.setViewName("addCrmAttention");
        return modelAndView;
    }

    /**
     * @Author zhoujinmu
     * @Description 用户对用户添加关注
     * @Date 2019/8/27 14:12
     */
    @RequestMapping(value = "/addAttention", method = RequestMethod.GET)
    public ModelAndView addStoreAttention(@RequestParam("attentionCrmId") Long attentionCrmId, HttpServletRequest httpServletRequest) {
        HttpSession httpSession = httpServletRequest.getSession();
        CrmDTO crmDTO = (CrmDTO) httpSession.getAttribute("crm");
        Long cid = crmDTO.getId();
        CrmCommentMappingDTO crmCommentMapping = new CrmCommentMappingDTO();
        crmCommentMapping.setCrmId(cid);
        crmCommentMapping.setAttentionCrmId(attentionCrmId);
        crmAttentionService.addCrmAttention(crmCommentMapping);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;
    }

    /**
     * @Author zhoujinmu
     * @Description 用户查看关注用户的店铺评价
     * @Date 2019/8/27 14:13
     */
    @RequestMapping(value = "/getAttentionStoreComment", method = RequestMethod.GET)
    public ModelAndView getAttentionStoreComment(HttpServletRequest httpServletRequest, @RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size) {
        HttpSession httpSession = httpServletRequest.getSession();
        CrmDTO crmDTO = (CrmDTO) httpSession.getAttribute("crm");
        Long cid = crmDTO.getId();
        List<OrderDTO> orderDTOS = crmAttentionService.getAttentionStoreComment(cid, page, size);
        List<CrmAttentionVO> crmAttentionVOS = new ArrayList<>();
        for (OrderDTO orderDTO : orderDTOS) {
            for (OrderDetailDTO orderDetail : orderDTO.getOrderDetailDTOList()) {
                CrmAttentionVO crmAttentionVO = new CrmAttentionVO();
                crmAttentionVO.setStoreComment(orderDetail.getStoreComment());
                crmAttentionVO.setStoreName(orderDetail.getStoreDTO().getName());
                crmAttentionVO.setStorePosition(orderDetail.getStoreDTO().getPosition());
                crmAttentionVOS.add(crmAttentionVO);
                System.out.println(crmAttentionVO);
            }
        }
//        PageInfo pageInfo = new PageInfo(c)
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("attentionCrmStoreComment");
        modelAndView.addObject("comment", crmAttentionVOS);
        return modelAndView;
    }



    /**
     * @param
     * @Description 获取当前关注列表
     * @author LiuHao
     * @createDate 2019/8/28
     **/
    @RequestMapping(value ="followlist",method = RequestMethod.GET)
    public ModelAndView getFollowed(HttpServletRequest httpServletRequest,
                                    @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(name = "size", required = false, defaultValue = "5") int size) {
        HttpSession httpSession = httpServletRequest.getSession();
        CrmDTO crmDTO = (CrmDTO) httpSession.getAttribute("crm");
        Long crmID = crmDTO.getId();
        List<CrmDTO> crmDTOList = crmAttentionService.getFollowed(crmID);

        List<CrmVO> crmVOList = new ArrayList<>();
        for (CrmDTO crm : crmDTOList) {
            crmVOList.add(CrmDTOTOCrmVo(crm));
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("FollowList");
        modelAndView.addObject("follow", crmVOList);
        return modelAndView;
    }

    /**
     * @param
     * @Description 获取用户租用过的车
     * @author LiuHao
     * @createDate 2019/8/28
     **/
    @RequestMapping(value = "followUsedCar")
    public ModelAndView getFollowedCar(Long crmID,
                                       @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                       @RequestParam(name = "size", required = false, defaultValue = "5") int size) {
        List<OrderVO> orderVOList= crmAttentionService.getUesdCar(crmID);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("FollowUsedCar");
        modelAndView.addObject("usedCar",  orderVOList);
        return modelAndView;
    }


    private CrmVO CrmDTOTOCrmVo(CrmDTO crmDTO) {
        CrmVO crmVO = new CrmVO();
        crmVO.setId(crmDTO.getId());
        crmVO.setUsername(crmDTO.getUsername());
        return crmVO;
    }

}
